//
//  SSCleanDetailHeaderView.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/2/14.
//

import UIKit

class SSCleanDetailHeaderView: UIView {
    
    var items: [ListCombo] = []
    var typeID:Int = 1
    var currentVC:UIViewController = UIViewController()
    
    class func headerView()->SSCleanDetailHeaderView{
        return Bundle.main.loadNibNamed("SSCleanDetailHeaderView", owner: nil, options: nil)?.last as! SSCleanDetailHeaderView
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    func setupUI() {
        addSubview(firstPartView)
        
        firstPartView.snp.makeConstraints { (m) in
            m.left.right.equalToSuperview().inset(16)
            m.height.equalTo(170)
            m.top.equalTo(0)
        }
    }
    
    func setTypeID(typeID:Int) {
        self.typeID = typeID
        if (typeID == 1 || typeID == 3) {
            addSubview(secondPartView)
//            secondPartView.backgroundColor = UIColor.orange
            secondPartView.snp.makeConstraints { (m) in
                m.left.right.equalToSuperview().inset(16)
                m.height.equalTo(10)
                m.top.equalTo(firstPartView.snp.bottom).offset(8)
            }
        }else if(typeID == 4){
            addSubview(secondPartView1)
//            secondPartView1.backgroundColor = UIColor.orange
            secondPartView1.snp.makeConstraints { (m) in
                m.left.right.equalToSuperview().inset(16)
                m.height.equalTo(40)
                m.top.equalTo(firstPartView.snp.bottom).offset(8)
            }
        }
        firstPartView.setType(type: typeID)
        
//        self.frame.size = CGSize(width: frame.width, height: 170 + 16 + 40)
//        print("")
    }
    
    lazy var firstPartView: SSCleanDetailFirstPartView = {
        let firstPartView = SSCleanDetailFirstPartView.firstPartView()
        return firstPartView
    }()
    
    lazy var secondPartView: SSCleanDetailSecondPartView = {
        let secondPartView = SSCleanDetailSecondPartView()
        secondPartView.numberResult { (number) in
            let vc = SSSingleMealViewController.init(nibName: "SSSingleMealViewController", bundle: nil)
            vc.id = self.items[number].id
            vc.rootId = "\(self.typeID)"
            self.currentVC.present(vc, animated: true)
            print("\(number)")
        }
        return secondPartView
    }()
    
    lazy var secondPartView1: SSCleanDetailSecondView1 = {
        let secondPartView = SSCleanDetailSecondView1.secondPartView1()
        return secondPartView
    }()

    func setItems(items:[ListCombo], typeID:Int) {

        if (self.typeID == 1 || self.typeID == 3) {
            self.items = items
            self.secondPartView.setItems(items: items)
            print("------secondPartView\(self.secondPartView.frame.maxY)")
            self.frame.size = CGSize(width: frame.width, height: secondPartView.frame.maxY)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        if self.typeID == 4 {
            self.frame.size = CGSize(width: frame.width, height: 170 + 16 + 40)
        }
    }

}
